package org.example.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.example.erp.domain.Inventory;
import org.example.erp.domain.WmProductSalse;
import org.example.erp.domain.WmProductSalseLine;
import org.example.erp.mapper.InventoryMapper;
import org.example.erp.mapper.WmProductSalseLineMapper;
import org.example.erp.service.WmProductSalseService;
import org.example.erp.mapper.WmProductSalseMapper;
import org.example.erp.utils.UtilMethod;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @author 28254
 * @description 针对表【wm_product_salse(销售出库单表)】的数据库操作Service实现
 * @createDate 2024-12-10 09:25:11
 */
@Service
@RequiredArgsConstructor
public class WmProductSalseServiceImpl extends ServiceImpl<WmProductSalseMapper, WmProductSalse>
        implements WmProductSalseService {
    private final WmProductSalseLineMapper wmProductSalseLineMapper;
    private final InventoryMapper inventoryMapper;

    @Override
    public void saveSalesOutbound(WmProductSalse wmProductSalse) {
        wmProductSalse.setSalseCode(UtilMethod.getRndomString("SLS"));
        this.save(wmProductSalse);
    }

    @Override
    public IPage<WmProductSalse> findSalesOutbound(Integer pageNum, Integer pageSize, String salesCode, String soCode, String clientName, String salseDate) {

        Page<WmProductSalse> page = Page.of(pageNum, pageSize);
        LambdaQueryWrapper<WmProductSalse> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(!"".equals(salesCode) && salesCode != null, WmProductSalse::getSalseCode, salesCode)
                .like(!"".equals(soCode) && soCode != null, WmProductSalse::getSoCode, soCode)
                .like(!"".equals(clientName) && clientName != null, WmProductSalse::getClientName, clientName)
                .like(!"".equals(salseDate) && salseDate != null, WmProductSalse::getSalseDate, salseDate);
        return baseMapper.selectPage(page, queryWrapper);
    }

    public void deleteSalesOutbound(List<Long> salseIds) {
        QueryWrapper<WmProductSalseLine> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("salse_id", salseIds);
        List<WmProductSalseLine> wmProductSalseLines = wmProductSalseLineMapper.selectList(queryWrapper);
        List<Long> list = wmProductSalseLines.stream().map(WmProductSalseLine::getLineId).toList();
        this.removeByIds(salseIds); //删除主表数据
        if(list.isEmpty())return;
        wmProductSalseLineMapper.deleteBatchIds(list); //删除从表数据
    }

    @Override
    public WmProductSalse querySalesOutbound(Long salesId) {
        WmProductSalse productSalse = this.getOne(new QueryWrapper<WmProductSalse>().eq("salse_id", salesId));
        List<WmProductSalseLine> lines = wmProductSalseLineMapper.selectList(new QueryWrapper<WmProductSalseLine>().eq("salse_id", salesId));
        if (productSalse != null) {
            productSalse.setWmProductSalseLines(lines);
        }
        return productSalse;
    }

    // 执行出库，如果出库数量不够则不能出库，还没完全解决
    @Override
    @Transactional
    public void shipments(WmProductSalse wmProductSalse) {
        wmProductSalse.setStatus("FINISHED");
        this.updateById(wmProductSalse);
        List<WmProductSalseLine> lines = wmProductSalseLineMapper.selectList(new QueryWrapper<WmProductSalseLine>().eq("salse_id", wmProductSalse.getSalseId()));
        for (WmProductSalseLine line : lines) {
            UpdateWrapper<Inventory> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("material_product_id", line.getMaterialStockId())
                    .ge("inventory_quantity", line.getQuantitySalse())
                    .setSql("inventory_quantity = inventory_quantity - {0}", line.getQuantitySalse());
            inventoryMapper.update(null, updateWrapper);
        }
    }
}




